package com.itheima.qingcheng.controller;

import com.itheima.qingcheng.aliyun.AliyunOssUtils;
import com.itheima.qingcheng.pojo.Brand;
import com.itheima.qingcheng.pojo.BrandQuery;
import com.itheima.qingcheng.pojo.PageBean;
import com.itheima.qingcheng.pojo.Result;
import com.itheima.qingcheng.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author zhangzhen
 * @version 1.0
 * @date 2025/1/21 9:05
 */
@Slf4j
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired private BrandService brandService;
    @Autowired
    private AliyunOssUtils aliyunOssUtils;
    @PutMapping("/update")
    public Result update(@RequestBody Brand brand)
    {
        log.info("Brand:{}",brand);
        brandService.updateById(brand);
        return  Result.success();
    }
    @PostMapping("/page")
    public PageBean findPage(@RequestParam("page") Integer pageSize,
                             @RequestParam("size") Integer size
            , @RequestBody BrandQuery brandQuery){

        PageBean page=brandService.findPage(pageSize,size,brandQuery);
        return  page;
    }
    @PostMapping("/upload")

    public Result upload(@RequestParam MultipartFile file)
    {
        log.info("file");
        String url = aliyunOssUtils.uploadFile(file);
        return Result.success(url);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand)
    {
        log.info("Brand:{}",brand);
        brandService.add(brand);
        return  new Result(1,"执行成功",null);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id)
    {
        log.info("id:{}",id);
        brandService.deleteById(id);
        return  Result.success();
    }
    @GetMapping("/{id}")
    public Result selectByid(@PathVariable("id")Integer id)
    {
        log.info("id:{}",id);
        Brand brand=brandService.selectById(id);
        return  Result.success(brand);

    }

}